//Please run our Campus Map applet from the Model.java file.

package CampusMap.Controller;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import CampusMap.Model.POI;
import CampusMap.Model.Point;
import CampusMap.Model.poiInOut;
import CampusMap.View.View;

public class Controller extends Observable implements ActionListener, MouseListener, MouseMotionListener {


	private ArrayList<Point> digList = new ArrayList<Point>();
	private ArrayList<POI> poiList = new ArrayList<POI>();
	protected int userX, userY;
	private int mouseX, mouseY;
	public String modeStatus;
	private poiInOut op1 = new poiInOut(poiList);
	private int curX=-1, curY=-1, poiIndex;
	private boolean inDrag = false;
	private int cursorStatus;
	//Create a file chooser
	final JFileChooser fc = new JFileChooser();
	private View view;
	
	public Controller(View v) {
		super();
		this.view = v;
		view.addMouseListener(this);
		view.addMouseMotionListener(this);
	
	}

	
	
	
	@Override
	public void mouseDragged(MouseEvent e) {
		//if statement so that the poi isn't moved during digitization
		if (modeStatus == "poi" & cursorStatus == 2){

			Point p = new Point(e.getX(),e.getY());


			curX = (int) p.getX();
			curY = (int) p.getY();
			if (inDrag) {
				poiList.get(poiIndex).getPt().setX(curX);
				poiList.get(poiIndex).getPt().setY(curY);

				//update file with new location
				op1.setPoiListin(poiList);	
				
				//Notify observers of change
				setChanged();		
				notifyObservers(poiList);
			}
		}
		
	}

	@Override
	public void mouseMoved(MouseEvent evt) {
		Point p = new Point(evt.getX(),evt.getY());
		
		  for (int i=0; i <= poiList.size()-1; i++){
		    if (poiList.get(i).isInside(p)){
		      view.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		      //cursorStatus will drive what the user is able to do when clicking
		      cursorStatus=2;
		   
		      break;
		    }
		    else{
		      view.setCursor(Cursor.getDefaultCursor());
		      cursorStatus=1;
		     
		      
		   
		    }
		  }
		
	}

	




	public int getCursorStatus() {
		return cursorStatus;
	}




	public void setCursorStatus(int cursorStatus) {
		this.cursorStatus = cursorStatus;
	}




	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent mousePress) {
		//if mode status is poi mode then we'll get the coords of the press
		if (modeStatus == "poi" & poiList.size() > 0){
			Point p = new Point(mousePress.getX(),mousePress.getY());
			
			//setting an index so we know which poi is going to get dragged
			for (int i=0; i <= poiList.size()-1; i++){
				if (poiList.get(i).isInside(p)){
					  poiIndex = i;
					break;
				}
			}
			//set flag that dragging might happen (used in the mouseDragged event) 
			inDrag = true;
		}
		
	}

	@Override
	public void mouseReleased(MouseEvent mUp) {
		//Get the mouse coordinates.
		mouseX =  mUp.getX();
		mouseY =  mUp.getY();
		modeStatus = view.modeStatus;
		
		//Actions if the "Digitize Path" button was pressed and the mouse button was released.
		if (modeStatus == "dig"){
			
			//Create Point from mouse coordinates.
			Point pnt = new Point((double) mouseX, (double) mouseY);
			
			//Add Point to the "Digitize Path" ArrayList.
			digList.add(pnt);
			
			//Print to console.
			System.out.println("");
			System.out.println("Digitized Path coordinates:");
			System.out.println(digList);	
			
			setChanged();		
			notifyObservers(digList);
		}
		
		//Actions if the "Enter Point of Interest" button was pressed and the mouse button was released.
		//But only if it's not a crosshair cursor
		if (modeStatus == "poi" & cursorStatus !=2){
			
			//Dialog box for user-specified Point of Interest name.
			String poiName = (String)JOptionPane.showInputDialog(
			                    null,
			                    "Enter a name for your Point of Interest.",
			                    "Point of Interest",
			                    JOptionPane.PLAIN_MESSAGE,
			                    null,
			                    null,
			                    null);
			
			
			//Create Point from mouse coordinates if string is not empty				
			if (poiName != null){
				Point pnt = new Point((double) mouseX, (double) mouseY);

				//Create POI from Point, hard-coded PointBuffer radius, Label.
				POI poi = new POI(pnt, (double) 8, poiName);

				//Add Point to the "Enter Point of Interest" ArrayList
				poiList.add(poi);
		

				//Print to console.
				System.out.println("");
				System.out.println("Points of Interest coordinates and name:");
				for (int i=0; i < poiList.size(); i++){
					System.out.println(poiList.get(i).getPt() + "   " + poiList.get(i).getLabel());	
					op1.setPoiListin(poiList);
				}				
				//if inDrag was set because you were dragging a poi, it now is unset
				inDrag = false;
			
				setChanged();		
				notifyObservers(poiList);
				
			}		
		}	
		
		//Notify observers of change
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

				
			

		
	}
}

